﻿using HcyWebAPI.Models;
using Microsoft.AspNetCore.Mvc;
using static HcyWebAPI.Controllers.Params.AccountParams;

namespace HcyWebAPI.Controllers
{
    [ApiController]
    [Route("api/account/")]
    public class AccountController : ControllerBase
    {
        private readonly MyDbContext _context = new MyDbContext();

        /// <summary>
        /// 查询所有用户 根据条件
        /// </summary>
        /// <param name="params"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("list")]
        public Models.ResultMsg GetAccountList(AccountListParams @params)
        {
            Models.ResultMsg resultMsg = new Models.ResultMsg();
            Models.ResultDataInfo resultDataInfo = new Models.ResultDataInfo();
            Func<Models.Account, bool> whereLam = (p => true);
            if (@params.username!=null&&@params.username != "")
            {
                whereLam = p => p.Username.Contains(@params.username);
            }
            if (@params.deptId != 0) { 
               whereLam= (p => p.DeptId==@params.deptId);
            }

            var temp = _context.Account.Where(whereLam)
                                     .OrderBy(p => p.Id)
                                     .Skip(@params.pageSize * (@params.pageIndex - 1))  //跳过多少
                                     .Take(@params.pageSize)                            //取多少
                                     .ToList();
            var count = _context.Account.Count(whereLam);
            //最后转为集合

            resultDataInfo.data = temp;
            resultDataInfo.total = count;
            resultMsg.data = resultDataInfo;
            return resultMsg;
        }

        /// <summary>
        /// 查询用户 根据指定ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("list/{id}")]
        public Models.Account? GetAccountById(int id)
        {
            int count = _context.Account.Count(b => b.Id == id);
            if (count == 0)
                return null;
            var person = _context.Account.Single(b => b.Id == id);
            List<Models.Account> peopleList = new List<Models.Account>();
            peopleList.Add(person);
            return person;
        }

        /// <summary>
        /// 删除用户 指定ID 
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("del")]
        public ResultMsg DeleteAccout(Models.Account account)
        {
            ResultMsg resultMsg = new ResultMsg();
            var temp = _context.Account.Single(p => p.Id == account.Id);
            _context.Account.Remove(temp);
            int result = _context.SaveChanges();
            resultMsg.data = result;
            return resultMsg;
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("update")]
        public ResultMsg UpdateAccount(Models.Account account)
        {
            ResultMsg resultMsg=new ResultMsg();
            _context.Update(account);
            int result = _context.SaveChanges();
            resultMsg.data = result;
            return resultMsg;
        }

    }
}
